###############################################################################
# R-code:	Run_vario.r
# Purpose:	For running 'vario_krige.R' functions to manually fit variogram model paramters.
# Specify:  Paths, files, and parameters in 'vario_krige.R'.
###############################################################################

#######
# PATHS
DirPrecipSourceVgmKrg	=	"C:\\Users\\bo_romero\\git\\fews-01-global-eval\\Global_Eval\\"
FilePrecipSourceVgmKrg	=	"vario_krige.r"

##################
# PRELIMINARY CODE
source(paste(DirPrecipSourceVgmKrg, FilePrecipSourceVgmKrg, sep=""))


########################################################################
########################################################################
########################################################################
# CODE FOR MANUALLY FITTING EMPIRICAL VARIOGRAM MODEL ... INTERACTIVELY.
########################################################################

# RUN ONLY ONCE per R session to define regions.
# May alternatively source as external file (e.g. defineRegions.R),
# that includes it's own parameters;
# If so, remove the function from krige_mons.R
defineRegions()

########################################
# Calculate average variogram for specified
# region, month, and data type (p=precipitation, t=temperature)

Mode 	= 	"p"
Region 	= 	Region_50S_0_60E_120E								
Month 	= 	1

fit_vario(Mode,Region,Month)

########################################
# Add new variogram line(s) to current plot.
#
# To quickly find good parameters
# without re-running initial variogram:
# -When prompted "Rerun with new paramaters? (y/n)"
# -Enter "n"
# -Then enter new parameters and re-run the code below, as necessary:

Nugget	=	0.0085
Sill	=	0.08			# This is the full sill, not the partial sill
Range	=	8

VgmModelForm 	= "Exp"		# Covariogram model form (as text, e.g. "Exp", "Gau", "Sph", "Mat")
MaxVarioDist 	= 14

# Set parameter to plot onto current plot, not new plot
par(new=T)

# Plot model as lines on variogram per new parameters above.
# Note that color below is random,
# so the line can be plotted several times
# to find it on the plot (by its changing colors)
# Note that the partial sill is required by the 'vgm' function,
# hence the 'Sill-Nugget' parameter.
lines(variogramLine(vgm(Sill-Nugget,VgmModelForm,Range,Nugget),MaxVarioDist), col=runif(1)*657)

########################################
# Output to .csv file for use in kriging.
# Write header row only once per output .csv file,
# write data rows for all variograms fit.

# Create directory for variogram parameters, if one does not already exist
dir.create(file.path(DirPrecipVgmPar), recursive=TRUE, showWarnings=FALSE)

# RUN ONLY ONCE per output .csv file, to write header row 
cat("Mode,Region,Month,Nugget,Sill,Range,VgmModelForm\n",
		file=paste(DirPrecipVgmPar,FilePrecipVgmPar,sep=""),
		append=FALSE)

# Write data row(s) of variograpm parameters
# (after the fit of the variogram is acceptable)
cat(Mode,",",Region$regionName,",",Month,",",Nugget,",",Sill,",",Range,",",VgmModelForm,"\n",
		file=paste(DirPrecipVgmPar,FilePrecipVgmPar,sep=""),
		append=TRUE)

# Plot the variogram to an image file
dev.print(png,file=paste(DirPrecipVgmPar,"Variogram_",Region$regionName,"_", sprintf("%02d", Month),".png", sep=""), width=480, height=480)

